package main

//command line execute "go install"
import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
)

func main() {
	var p string
	if len(os.Args) <= 1 {
		os.Exit(1)
	}

	p = os.Args[1]

	dir := filepath.Dir(p)
	//fmt.Println(filepath.Base(dir)) //get the
	//fmt.Println("args len:", len(os.Args), "os.Args:", os.Args)

	cmd := exec.Command("cmd", "/C", "go", "install")
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	cmd.Dir = dir //os.Getenv("GOPATH") + "/bin"
	err := cmd.Run()
	//os.Exit(0)
	if err != nil {
		fmt.Println(err)
		os.Exit(2)
	}

	//run the pack
	packName := filepath.Base(dir)
	//fmt.Println(packName)

	cmdRunPack := exec.Command("cmd", "/C", "start", packName)
	cmdRunPack.Stderr = os.Stderr
	cmdRunPack.Stdout = os.Stdout
	cmdRunPack.Dir = os.Getenv("GOPATH") + "/bin"

	err = cmdRunPack.Run()

	if err != nil {
		fmt.Println(err)
		os.Exit(3)
	}

	os.Exit(0)
}
